﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;

// Token: 0x02000232 RID: 562
public class SimCam : MonoBehaviour
{
	// Token: 0x06000BD4 RID: 3028 RVA: 0x000623B8 File Offset: 0x000605B8
	public void Enable()
	{
		this.enable = true;
		this.zoomPower = 0f;
		this.movementDirection = Vector3.zero;
		this.GetCameraHeight();
		this.baseSpeed = this.movementSpeed * this.cameraHeight;
		this.edgeScroll = GlobalPreferences.CameraEdgeScroll.value;
	}

	// Token: 0x06000BD5 RID: 3029 RVA: 0x0006240B File Offset: 0x0006060B
	public void Disable()
	{
		this.enable = false;
	}

	// Token: 0x06000BD6 RID: 3030 RVA: 0x00062414 File Offset: 0x00060614
	private void Start()
	{
		if (GlobalPreferences.CameraRtsMode.value)
		{
			this.mode = SimCam.Mode.Basic;
		}
		else
		{
			this.mode = SimCam.Mode.Advanced;
		}
		this.Disable();
		this.mainCamera = Camera.main;
		this.cameraTransform = this.mainCamera.transform.parent;
		this.mouseSensibility = GlobalPreferences.MouseSensibility.value;
		this.edgeScroll = GlobalPreferences.CameraEdgeScroll.value;
		this.baseSpeed = this.movementSpeed;
		this.eventSystem = EventSystem.current;
	}

	// Token: 0x06000BD7 RID: 3031 RVA: 0x000624A1 File Offset: 0x000606A1
	private void Update()
	{
		if (!this.enable || GameController.Instance.paused || GameController.Instance.userIsTyping)
		{
			return;
		}
		this.GetInput();
		this.UpdateCenterPoint();
		this.GetCameraHeight();
	}

	// Token: 0x06000BD8 RID: 3032 RVA: 0x000624E0 File Offset: 0x000606E0
	private void LateUpdate()
	{
		if (!this.enable || GameController.Instance.paused)
		{
			return;
		}
		if (Time.timeScale == 0f)
		{
			return;
		}
		if (this.mode == SimCam.Mode.Basic)
		{
			this.BasicMode();
		}
		else
		{
			this.AdvancedMode();
		}
		this.Zoom();
		this.MouseLook();
	}

	// Token: 0x06000BD9 RID: 3033 RVA: 0x00062540 File Offset: 0x00060740
	private void BasicMode()
	{
		if (!this.OrbitPoint(this.centerPoint))
		{
			this.MoveBasic();
		}
	}

	// Token: 0x06000BDA RID: 3034 RVA: 0x00062559 File Offset: 0x00060759
	private void AdvancedMode()
	{
		if (!this.OrbitPoint(this.centerPoint))
		{
			this.MoveAdvanced();
		}
	}

	// Token: 0x06000BDB RID: 3035 RVA: 0x00062574 File Offset: 0x00060774
	private void GetInput()
	{
		this.mousePositionInScreen = Input.mousePosition;
		this.rightClick = Input.GetButton(ButtonInput.Aim);
		this.middleClick = Input.GetButton(ButtonInput.LookBack);
		this.mouseH = Input.GetAxis(ButtonInput.MouseX) * this.mouseSensibility;
		this.mouseV = Input.GetAxis(ButtonInput.MouseY) * this.mouseSensibility;
		this.shift = Input.GetButton(ButtonInput.Sprint);
		this.decreaseSpeed = Input.GetButton(ButtonInput.SizeDown);
		this.increaseSpeed = Input.GetButton(ButtonInput.SizeUp);
		this.zoomInput = Input.GetAxisRaw(ButtonInput.MouseScrollWheel);
		this.forwardMovement = Input.GetAxis(ButtonInput.VerticalAxis);
		this.righMovement = Input.GetAxis(ButtonInput.HorizontalAxis);
		if (this.eventSystem.IsPointerOverGameObject() || this.MouseOutOfScreen())
		{
			this.zoomInput = 0f;
		}
		if (this.edgeScroll && !this.rightClick && this.mode == SimCam.Mode.Basic && Screen.fullScreen)
		{
			if (this.forwardMovement == 0f)
			{
				if (this.mousePositionInScreen.y < (float)this.borderThickness)
				{
					this.forwardMovement = -1f;
				}
				if (this.mousePositionInScreen.y > (float)(Screen.height - this.borderThickness))
				{
					this.forwardMovement = 1f;
				}
			}
			if (this.righMovement == 0f)
			{
				if (this.mousePositionInScreen.x < (float)this.borderThickness)
				{
					this.righMovement = -1f;
				}
				if (this.mousePositionInScreen.x > (float)(Screen.width - this.borderThickness))
				{
					this.righMovement = 1f;
				}
			}
		}
		this.upMovement = 0f;
		if (Input.GetKey(KeyCode.E))
		{
			this.upMovement += 1f;
		}
		if (Input.GetKey(KeyCode.Q))
		{
			this.upMovement -= 1f;
		}
		if (Input.GetButtonDown(ButtonInput.CameraSwitch))
		{
			if (this.mode == SimCam.Mode.Basic)
			{
				this.mode = SimCam.Mode.Advanced;
			}
			else
			{
				this.mode = SimCam.Mode.Basic;
			}
			GlobalPreferences.CameraRtsMode.value = (this.mode == SimCam.Mode.Basic);
		}
	}

	// Token: 0x06000BDC RID: 3036 RVA: 0x000627C8 File Offset: 0x000609C8
	private bool MouseOutOfScreen()
	{
		Vector2 vector = Input.mousePosition;
		return vector.x > (float)Screen.width || vector.x < 0f || vector.y > (float)Screen.height || vector.y < 0f;
	}

	// Token: 0x06000BDD RID: 3037 RVA: 0x0006282C File Offset: 0x00060A2C
	private void MoveCamera(Vector3 forward, Vector3 up, Vector3 right)
	{
		if (this.forwardMovement != 0f || this.righMovement != 0f || this.upMovement != 0f)
		{
			this.movementDirection = this.forwardMovement * forward + this.righMovement * right + this.upMovement * up;
			this.movementDirection.Normalize();
			this.movementDirection *= this.currentSpeed;
			Vector3 b = this.movementDirection * Time.deltaTime;
			this.cameraTransform.position += b;
			return;
		}
	}

	// Token: 0x06000BDE RID: 3038 RVA: 0x000628F0 File Offset: 0x00060AF0
	private bool OrbitPoint(Vector3 point)
	{
		Vector3 vector = this.cameraTransform.position - point;
		Vector3 eulerAngles = Quaternion.LookRotation(vector).eulerAngles;
		if (!this.middleClick)
		{
			this.myRotation = Quaternion.Euler(eulerAngles.x, eulerAngles.y, 0f);
			this.targetRotation = this.myRotation;
			return false;
		}
		float x = this.targetRotation.eulerAngles.x + this.mouseV;
		float y = this.targetRotation.eulerAngles.y + this.mouseH;
		x = this.ClampAngle(x);
		this.targetRotation = Quaternion.Euler(x, y, 0f);
		this.myRotation = Quaternion.Slerp(this.myRotation, this.targetRotation, this.rotationSmooth * Time.deltaTime);
		vector = this.myRotation * (Vector3.forward * vector.magnitude);
		this.cameraTransform.position = vector + point;
		this.LookAt(point);
		return true;
	}

	// Token: 0x06000BDF RID: 3039 RVA: 0x00062A04 File Offset: 0x00060C04
	private void LookAt(Vector3 point)
	{
		Vector3 forward = point - this.cameraTransform.position;
		this.cameraTransform.rotation = Quaternion.LookRotation(forward);
	}

	// Token: 0x06000BE0 RID: 3040 RVA: 0x00062A34 File Offset: 0x00060C34
	private void MoveBasic()
	{
		Vector3 forward = this.cameraTransform.forward;
		forward.y = 0f;
		forward.Normalize();
		Vector3 right = this.cameraTransform.right;
		right.y = 0f;
		right.Normalize();
		this.baseSpeed = this.movementSpeed * this.cameraHeight;
		this.currentSpeed = this.baseSpeed;
		if (this.shift)
		{
			this.currentSpeed *= this.shiftMultiplier;
		}
		this.MoveCamera(forward, Vector3.up, right);
	}

	// Token: 0x06000BE1 RID: 3041 RVA: 0x00062ACC File Offset: 0x00060CCC
	private void MoveAdvanced()
	{
		if (this.increaseSpeed)
		{
			this.baseSpeed *= 1f + this.speedChange * Time.deltaTime;
		}
		if (this.decreaseSpeed)
		{
			this.baseSpeed /= 1f + this.speedChange * Time.deltaTime;
		}
		this.currentSpeed = this.baseSpeed;
		if (this.shift)
		{
			this.currentSpeed *= this.shiftMultiplier;
		}
		this.MoveCamera(this.cameraTransform.forward, this.cameraTransform.up, this.cameraTransform.right);
	}

	// Token: 0x06000BE2 RID: 3042 RVA: 0x00062B80 File Offset: 0x00060D80
	private void UpdateCenterPoint()
	{
		if (this.middleClick)
		{
			return;
		}
		Vector3 pos = new Vector3((float)(Screen.width / 2), (float)(Screen.height / 2), 0f);
		Ray ray = this.mainCamera.ScreenPointToRay(pos);
		RaycastHit raycastHit;
		if (Physics.Raycast(ray, out raycastHit, float.PositiveInfinity))
		{
			this.centerPoint = raycastHit.point;
		}
	}

	// Token: 0x06000BE3 RID: 3043 RVA: 0x00062BE4 File Offset: 0x00060DE4
	private void GetCameraHeight()
	{
		if (this.cameraTransform == null)
		{
			this.cameraHeight = 1f;
			return;
		}
		Vector3 position = this.cameraTransform.position;
		RaycastHit raycastHit;
		if (Physics.Raycast(position, Vector3.down, out raycastHit, 10000f))
		{
			this.cameraHeight = raycastHit.distance;
		}
	}

	// Token: 0x06000BE4 RID: 3044 RVA: 0x00062C40 File Offset: 0x00060E40
	private void Zoom()
	{
		if (this.zoomInput != 0f)
		{
			this.zoomPower = this.zoomInput;
			if (this.shift)
			{
				this.zoomPower *= this.zoomShift;
			}
		}
		if (this.zoomPower == 0f)
		{
			return;
		}
		Vector3 vector = this.centerPoint - this.cameraTransform.position;
		float magnitude = vector.magnitude;
		float num = this.zoomPower * this.zoomSpeed * magnitude;
		this.zoomPower = Mathf.Lerp(this.zoomPower, 0f, this.zoomSmooth * Time.deltaTime);
		if (magnitude - num > 0f)
		{
			vector = vector.normalized * num;
			if (this.shift)
			{
				vector *= this.shiftMultiplier;
			}
			this.cameraTransform.position += vector;
		}
	}

	// Token: 0x06000BE5 RID: 3045 RVA: 0x00062D34 File Offset: 0x00060F34
	private void MouseLook()
	{
		if (this.rightClick)
		{
			float x = this.cameraTransform.localEulerAngles.x - this.mouseV * this.fpsSensibility;
			x = this.ClampAngle(x);
			float y = this.cameraTransform.localEulerAngles.y + this.mouseH * this.fpsSensibility;
			this.cameraTransform.rotation = Quaternion.Slerp(this.cameraTransform.rotation, Quaternion.Euler(x, y, 0f), this.rotationSmooth * Time.deltaTime);
		}
	}

	// Token: 0x06000BE6 RID: 3046 RVA: 0x00062DCB File Offset: 0x00060FCB
	private float ClampAngle(float x)
	{
		if (x > 180f)
		{
			x -= 360f;
		}
		x = Mathf.Clamp(x, -89f, 89f);
		return x;
	}

	// Token: 0x04001118 RID: 4376
	private bool enable;

	// Token: 0x04001119 RID: 4377
	private Camera mainCamera;

	// Token: 0x0400111A RID: 4378
	private EventSystem eventSystem;

	// Token: 0x0400111B RID: 4379
	private float zoomSpeed = 0.3f;

	// Token: 0x0400111C RID: 4380
	private float zoomPower;

	// Token: 0x0400111D RID: 4381
	public float zoomSmooth = 5f;

	// Token: 0x0400111E RID: 4382
	public float rotationSmooth = 16f;

	// Token: 0x0400111F RID: 4383
	public float zoomShift = 1.2f;

	// Token: 0x04001120 RID: 4384
	private float shiftMultiplier = 5f;

	// Token: 0x04001121 RID: 4385
	private float movementSpeed = 1f;

	// Token: 0x04001122 RID: 4386
	private float currentSpeed;

	// Token: 0x04001123 RID: 4387
	private float baseSpeed;

	// Token: 0x04001124 RID: 4388
	private Vector3 movementDirection;

	// Token: 0x04001125 RID: 4389
	private Transform cameraTransform;

	// Token: 0x04001126 RID: 4390
	private SimCam.Mode mode;

	// Token: 0x04001127 RID: 4391
	private float mouseSensibility;

	// Token: 0x04001128 RID: 4392
	private float fpsSensibility = 2f;

	// Token: 0x04001129 RID: 4393
	private float cameraHeight = 1f;

	// Token: 0x0400112A RID: 4394
	public float speedChange = 2.5f;

	// Token: 0x0400112B RID: 4395
	private bool shift;

	// Token: 0x0400112C RID: 4396
	private bool decreaseSpeed;

	// Token: 0x0400112D RID: 4397
	private bool increaseSpeed;

	// Token: 0x0400112E RID: 4398
	private float zoomInput;

	// Token: 0x0400112F RID: 4399
	private float forwardMovement;

	// Token: 0x04001130 RID: 4400
	private float righMovement;

	// Token: 0x04001131 RID: 4401
	private float upMovement;

	// Token: 0x04001132 RID: 4402
	private Vector3 mousePositionInScreen;

	// Token: 0x04001133 RID: 4403
	private Vector3 centerPoint;

	// Token: 0x04001134 RID: 4404
	private bool rightClick;

	// Token: 0x04001135 RID: 4405
	private bool middleClick;

	// Token: 0x04001136 RID: 4406
	private float mouseH;

	// Token: 0x04001137 RID: 4407
	private float mouseV;

	// Token: 0x04001138 RID: 4408
	private int borderThickness = 3;

	// Token: 0x04001139 RID: 4409
	private Quaternion myRotation;

	// Token: 0x0400113A RID: 4410
	private Quaternion targetRotation;

	// Token: 0x0400113B RID: 4411
	private bool edgeScroll;

	// Token: 0x02000233 RID: 563
	private enum Mode
	{
		// Token: 0x0400113D RID: 4413
		Basic,
		// Token: 0x0400113E RID: 4414
		Advanced
	}
}
